#ifndef _GAME_H
#define _GAME_H

#include "../includes.h"

void MainLoop(void)
{
    //{ ********** load and initialise media **********

    // set up player object
    cPlayer player("media/ship_scaled.png", 1, 1);
    player.startWave();

    //playerBullets
    vector<cPlayerBullet> playerBullets;
    for (short x = 0; x <= 10; x++)
        playerBullets.push_back(cPlayerBullet("media/bullet.png", 3, findFreeSprite(), player));
    cPlayerBullet::lastFired = dbTimer();

    // pirates
    vector<cEnemy> pirates;
    for (short x = 0; x <= 6; x++)
        pirates.push_back(cEnemy("media/pirate.png", 4, findFreeSprite(), cEnemy::pirate, 500, 500, 270.0f));

    // load and set up aim reticle
    dbLoadImage("media/reticle.png", 2); dbSprite(2, 0, 0, 2); dbOffsetSprite(2, dbImageWidth(2)/2, dbImageHeight(2)/2);
    dbSetSpritePriority(2, 2); // priority is 2, so it gets drawn above enemies and stuff.

    //} ********** load and initialise media **********

    // initialise timerBased command, so that the timer is set at first loop, prevents strange sprite behaviour
    timerBased(0.0f,true);

    do
    {

        // ********** if a wave is active, not end of wave **********
        if (player.inWave)
        {
            //{ ********** update player positions **********
            // get angle between player and mouse
            player.angle = dbATanFull(dbMouseX() - player.X, player.Y - dbMouseY());

            // if the player is at the mouse position, don't move the player position, prevents jittering player sprite
            if (!(dbSpriteCollision(player.spriteID, 2)))
            {
                // update player x and y values based based on angle and keys pressed
                player.X += timerBased(
                            ((dbKeyState(player.backKey) - dbKeyState(player.foreKey)) * dbCos(player.angle + 90.0f)) * player.speed
                            );
                player.Y += timerBased(
                            ((dbKeyState(player.backKey) - dbKeyState(player.foreKey)) * dbSin(90.0f - player.angle)) * player.speed
                            );
            }
            player.update();

            // update aim reticle
            dbSprite(2, dbMouseX(), dbMouseY(), 2);

            //} ********** update player positions **********


            //{ ********** update bullet **********
            if (dbMouseClick()==1 && dbTimer() - cPlayerBullet::lastFired >= 500)
                playerBullets[cPlayerBullet::nextBullet].fire(player);

            for (unsigned int x=0; x < playerBullets.size(); x++)
                playerBullets[x].update();
            //} ********** update bullet **********


            //{ ********** update enemies **********
            for (unsigned short x = 0; x < pirates.size(); x++)
            {
                pirates[x].update();

                // enemy collisions
                if(!pirates[x].hidden && dbSpriteCollision(pirates[x].spriteID, 0))
                {
                    // enemy + player bullet collisions
                    for(unsigned short y = 0; y < playerBullets.size(); y++)
                    {
                        if (!playerBullets[y].hidden && dbSpriteCollision(pirates[x].spriteID, playerBullets[y].spriteID))
                        {
                            dbHideSprite(pirates[x].spriteID); pirates[x].hidden = true;
                            dbHideSprite(playerBullets[y].spriteID); playerBullets[y].hidden = true;
                            break;
                        }
                    }
                }

            }

            //} ********** update enemies **********


            dbText(0,0,dbStr(dbScreenFPS()));
            // reset timer for timerbased
            timerBased(0.0f,true);
        }
        // ********** if a wave is active, not end of wave **********

        // ********** if a wave isn't active, end of wave **********
        else
        {
        }
        // ********** if a wave isn't active, end of wave **********

// *****************************************************************
        // if escape key is pressed, go back to menu
        if(dbEscapeKey())
        {
            flushGameMedia();
            break;
        }
        dbSync();
    }
    while (windowEvent() != WM_CLOSE);
    return;
}

void flushGameMedia(void)
{
    if(dbIsImage(2))
    {
        dbDeleteSprite(2);
        dbDeleteImage(2);
    }
}

//{ ********** image IDs **********
/*
    player = 1
    aim reticle = 2
    bullet = 3
    pirate = 4
    piratebullet = 5
*/
//}

#endif
